<?php

class myUser extends sfBasicSecurityUser {

    public function signIn($user, $basicAuth) {
        $this->setAttribute('user_id', $user->id, 'yougoGuardSecurityUser');
        $this->setAttribute('user_email', $user->email, 'yougoGuardSecurityUser');
        $this->setAttribute('user_fullName', $user->fullName, 'yougoGuardSecurityUser');
        $this->setAttribute('user_typeId', $user->typeId, 'yougoGuardSecurityUser');
        $this->setAttribute('user_basicAuth', $basicAuth, 'yougoGuardSecurityUser');
        $this->setAuthenticated(true);
        $this->clearCredentials();
        if ($user->admin)
            $this->addCredential('admin');
    }

    public function signOut() {
        $this->getAttributeHolder()->removeNamespace('yougoGuardSecurityUser');
        $this->clearCredentials();
        $this->setAuthenticated(false);
    }

    public function updatePassword($password) {
        $this->setAttribute('user_basicAuth', base64_encode($this->getEmail().':'.$password), 'yougoGuardSecurityUser');
    }

    public function isAdmin() {
        $this->hasCredentials('admin');
    }

    public function getId() {
        return $this->getAttribute('user_id', '', 'yougoGuardSecurityUser');
    }

    public function getEmail() {
        return $this->getAttribute('user_email', '', 'yougoGuardSecurityUser');
    }

    public function getFullName() {
        return $this->getAttribute('user_fullName', '', 'yougoGuardSecurityUser');
    }

    public function getTypeId() {
        return $this->getAttribute('user_typeId', '', 'yougoGuardSecurityUser');
    }

    public function getBasicAuth() {
        return $this->getAttribute('user_basicAuth', '', 'yougoGuardSecurityUser');
    }

    public function setFlashGlobalError($message = 'Sorry, something went wrong. A team of highly trained monkeys has been dispatched to deal with this situation.') {
        $this->setFlash('globalError', $message);
    }

    public function setFlashError($message = 'Sorry, something went wrong. A team of highly trained monkeys has been dispatched to deal with this situation.') {
        $this->setFlash('error', $message);
    }

    public function hasNonEmptyFlash($name) {
        return parent::hasFlash($name) && parent::getFlash($name) != '';
    }

}
